/**
 * Created by xc on 15/12/22.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbFeedbackModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  //`from` varchar(100) NOT NULL DEFAULT 'app' COMMENT '反馈来源',
  //`like_design` int(1) DEFAULT NULL COMMENT '0喜欢／1还行／2不喜欢',
  //`like_product` int(1) DEFAULT NULL COMMENT '0喜欢／1还行／2不喜欢',
  //`advice` text COMMENT '意见反馈文字信息',
  //`images` text COMMENT '意见反馈添加截图的 url 数组的 json 字符串',
  //`device_info_user` text COMMENT '设备信息:用户选填',
  //`device_info_sys` text COMMENT '设备信息:自动收集(json字符串)',
  //`mail` varchar(100) DEFAULT NULL COMMENT '邮箱',
  //`mobile` bigint(20) DEFAULT NULL COMMENT '手机号',
  //`user_id` bigint(20) DEFAULT NULL,
  //`state` int(2) NOT NULL DEFAULT '0' COMMENT '状态',
  //`note` text COMMENT '后台用备注',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) DEFAULT NULL,
  //`del_flg` int(1) NOT NULL DEFAULT '0',


};
ItbFeedbackModel.prototype = new ItbBaseModel();

// 取得表名
ItbFeedbackModel.prototype.getTableName = function(){
    return 'itb_feedback';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbFeedbackModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}
// 删除
ItbFeedbackModel.prototype.deleteInfoLogic = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = '';
        var sqlValue = '';
        if (THIS.user_id != null && THIS.user_id.length > 0) {
            sql = 'UPDATE '+tableName+' SET del_flg=1, update_time=sysdate()' +
                ' WHERE user_id=? ';
            sqlValue = [THIS.user_id];

        } else if (THIS.mail != null && THIS.mail.length > 0) {
            sql = 'UPDATE '+tableName+' SET del_flg=1, update_time=sysdate()' +
                ' WHERE mail=? ';
            sqlValue = [THIS.mail];

        } else {
            //sql = 'UPDATE '+tableName+' SET del_flg=1, update_time=sysdate() ';
            //sqlValue = [];
            return callback("参数错误");
        }

        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            if(error) {
                callback(error);
            } else {
                callback(null);
            }
        });
    });
}

// 导出
module.exports = ItbFeedbackModel;